import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.List;
import java.util.Set;

public class test_2025_11_27 {
    private WebDriver driver = null;
    public void start()
    {
//        1、打开浏览器（驱动）
        WebDriverManager.chromedriver().setup();
//        2)添加浏览器配置
        ChromeOptions options = new ChromeOptions();
//        允许访问所有链接
        options.addArguments("--remote-allow-origins=*");
        //WebDriver driver = new ChromeDriver(options);
        //ChromeDriver driver = new ChromeDriver(options);
        driver = new ChromeDriver(options);

    }

    public void stop()
    {
        driver.quit();
    }


    //点击 click
    public void test_click() throws InterruptedException {
        start();

        //打开百度页面
        driver.get("https://www.baidu.com/");
        Thread.sleep(3000);
//        //查找到百度一下按钮
//        WebElement ele =  driver.findElement(By.cssSelector("#chat-submit-button"));
//        //点击百度一下按钮
//        ele.click();

        //找到右上角的图片超链接
        //点击
//        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
//        Thread.sleep(3000);


        //不可点击的元素（报错）
        WebElement ele = driver.findElement(By.cssSelector("#s_is_result_css"));
        ele.click();

        stop();
    }

    public void test_sendKeys() throws InterruptedException {
        start();
        driver.get("https://www.baidu.com/");
        Thread.sleep(3000);
//        //查找百度输入框
//        WebElement ele = driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]"));
//        //输入关键词
//        ele.sendKeys("邓紫棋");

        //查找百度一下按钮并输入关键词(不会报错)
//        driver.findElement(By.cssSelector("#chat-submit-button")).sendKeys("邓紫棋");

        //不可交互的元素（报错）
        driver.findElement(By.cssSelector("#s_is_result_css")).sendKeys("邓紫棋");
        Thread.sleep(3000);
        stop();
    }

    public void test_clear() throws InterruptedException {
        start();
        driver.get("https://www.baidu.com/");
        Thread.sleep(3000);
        //查找百度输入框并输入关键词
        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]")).sendKeys("邓紫棋");
        //查找百度一下按钮并点击
        driver.findElement(By.cssSelector("#chat-submit-button")).click();
        Thread.sleep(2000);

        //清空百度输入框
        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]")).clear();
        Thread.sleep(2000);

        //查找迪丽热巴
        driver.findElement(By.xpath("//*[@id=\"chat-textarea\"]")).sendKeys("迪丽热巴");
        //查找百度一下按钮并点击
        driver.findElement(By.cssSelector("#chat-submit-button")).click();

        Thread.sleep(5000);
        stop();
    }

    public void test_getText() throws InterruptedException {
        start();
        driver.get("https://www.baidu.com/");
        Thread.sleep(3000);

//        //找到百度一下按钮
//        WebElement ele = driver.findElement(By.cssSelector("#chat-submit-button"));
//        //检查按钮是否正确：校验“百度一下”文本
//        String btn_text = ele.getText();
//        System.out.println(btn_text);


//        //批量获取所有的新闻
//        List<WebElement> eles = driver.findElements(By.cssSelector("#hotsearch-content-wrapper > li > a > span.title-content-title"));
//        for(WebElement ele : eles)
//        {
//            System.out.println(ele.getText());
//        }

        //找到百度输入框获取文本
//        String text = driver.findElement(By.cssSelector("#chat-textarea")).getText();
        String text = driver.findElement(By.cssSelector("#chat-textarea")).getAttribute("placeholder");
        System.out.println("文本为:"+text);

        Thread.sleep(3000);
        stop();
    }

    public void test_driver() throws InterruptedException {
        start();
        driver.get("https://www.baidu.com/");
        Thread.sleep(3000);

//        //获取页面标题
//        String title = driver.getTitle();
//        //获取页面URL
//        String url = driver.getCurrentUrl();
//
//        System.out.println("title:"+title);
//        System.out.println("url:"+url);//https://www.baidu.com/
//
//        //通过页面元素来获取标题(无法打印）
//        String eleText = driver.findElement(By.cssSelector("head > title")).getText();
//        System.out.println("页面元素获取页面标题:"+eleText);


        //获取页面标题
        String title = driver.getTitle();
        //获取页面URL
        String url = driver.getCurrentUrl();

        System.out.println("跳转之前：");
        System.out.println("标题:"+title+",URL:"+url);


//        //访问百度页面，搜索关键词“邓紫棋”，检查是否成功搜索
//        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("邓紫棋");
//        driver.findElement(By.cssSelector("#chat-submit-button")).click();

        //点击百度图片超链接进入百度图片页面
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();

        Thread.sleep(5000);
        //页面发生变化：标题+URL+内容
        //获取页面标题
        String jump_title = driver.getTitle();
        //获取页面URL
        String jump_url = driver.getCurrentUrl();

        System.out.println("跳转之后：");
        System.out.println("标题:"+jump_title+",URL:"+jump_url);


        Thread.sleep(3000);
        stop();
    }

    public void test_handle() throws InterruptedException {
        start();
        driver.get("https://www.baidu.com/");

        //获取页面标题
        String title = driver.getTitle();
        //获取页面URL
        String url = driver.getCurrentUrl();

        System.out.println("跳转之前：");
        System.out.println("标题:"+title+",URL:"+url);

        //点击百度图片超链接进入百度图片页面
        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();


        //打开了多个标签页——需要切换句柄
        String cur_handle = driver.getWindowHandle();
        Set<String> all_handle = driver.getWindowHandles();
        System.out.println("当前页面的句柄："+cur_handle);
        System.out.println("所有页面的句柄："+all_handle);

        for (String handle : all_handle)
        {
            if(!handle.equals(cur_handle))
            {
                //切换句柄(driver从百度首页切换到了百度图片页面)
                driver.switchTo().window(handle);
            }
        }

        //获取页面标题
        String jump_title = driver.getTitle();
        //获取页面URL
        String jump_url = driver.getCurrentUrl();

        System.out.println("跳转之后：");
        System.out.println("标题:"+jump_title+",URL:"+jump_url);

        Thread.sleep(2000);

        driver.get("https://www.hao123.com/?src=from_pc");
        Thread.sleep(3000);

        stop();
    }

    public void test_quit() throws InterruptedException {
        start();

//        //多个标签页
//        driver.get("https://www.baidu.com/");
//        //点击百度图片超链接进入百度图片页面
        Thread.sleep(3000);
//        driver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
//
//
//        Thread.sleep(3000);
//        driver.close();  //把百度首页标签页关闭掉了


        //此时还有百度图片标签页
//        System.out.println(driver.getTitle());
        driver.quit();
    }
    public void test_windowSize() throws InterruptedException {
        start();

        //窗口最大化
        Thread.sleep(3000);
        driver.manage().window().maximize();
        //窗口最小化
        Thread.sleep(3000);
        driver.manage().window().minimize();
        //窗口全屏
        Thread.sleep(3000);
        driver.manage().window().fullscreen();
        //手动设置大小
        Thread.sleep(3000);
        driver.manage().window().setSize(new Dimension(1024,768));

        Thread.sleep(3000);
        stop();
    }
}
